import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ToastProvider extends StatefulWidget {
  final Widget child;
  ToastProvider({this.child});
  @override
  State<StatefulWidget> createState() {
    return _ToastProviderState();
  }
}

// 需要暴露的方法
Function(Widget) showToast; // 显示
Future Function() hideToast; // 隐藏
bool Function() getToastVisible; //获取是否正在显示

const int _animateDuration = 150; // 渐变动画时长
const int _minDialogDuration = 300; // toast最小持续时长

class _ToastProviderState extends State<ToastProvider>
    with SingleTickerProviderStateMixin {
  bool _visible = false;
  Widget _child = Text('');
  double _animatePercent = 0;
  var _showTime = new DateTime.now().millisecondsSinceEpoch;
  AnimationController _animatedContainer;

  @override
  void initState() {
    // 初始化动画
    _animatedContainer = new AnimationController(
        duration: Duration(milliseconds: _animateDuration), vsync: this);
    // 监听动画
    _animatedContainer.addListener(() {
      var per = _animatedContainer.value;
      setState(() {
        _animatePercent = per;
      });
    });

    // 实现暴漏的方法
    showToast = (child) {
      setState(() {
        _visible = true;
        _child = child;
      });
      _animatedContainer.forward(from: 0);
      _showTime = DateTime.now().millisecondsSinceEpoch;
    };

    hideToast = () {
      var now = DateTime.now().millisecondsSinceEpoch;
      var diff = now - _showTime;
      var minDur = _minDialogDuration + _animateDuration;
      var mill = diff > minDur ? 0 : minDur - diff;
      return Future.delayed(
        Duration(milliseconds: mill),
        () => _animatedContainer.reverse(from: 1),
      ).then((_) {
        setState(() {
          _visible = false;
        });
      });
    };

    getToastVisible = () {
      return _visible;
    };

    super.initState();
  }

  // 销毁动画监听
  @override
  void dispose() {
    _animatedContainer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack(
        children: [
          widget.child,
          Visibility(
            visible: _visible,
            child: Positioned(
              top: 0,
              left: 0,
              right: 0,
              bottom: 0,
              child: Opacity(
                opacity: _animatePercent,
                child: Container(
                  color: Colors.black38,
                  child: Transform.scale(
                    scale: 1.08 - _animatePercent * 0.08,
                    child: Transform.translate(
                      offset: Offset(0, 30 - _animatePercent * 30),
                      child: _ToastChild(_child),
                    ),
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

class _ToastChild extends StatelessWidget {
  final Widget child;
  _ToastChild(this.child);
  @override
  Widget build(BuildContext context) {
    return Theme(
      data: ThemeData.dark(),
      child: Scaffold(
        backgroundColor: Color.fromARGB(0, 0, 0, 0),
        body: Center(
          child: Container(
            child: child,
            padding: EdgeInsets.fromLTRB(14, 10, 14, 10),
            decoration: BoxDecoration(
              color: Color.fromARGB(233, 31, 36, 40),
              borderRadius: BorderRadius.circular(4),
            ),
          ),
        ),
      ),
    );
  }
}
